<template>
	<view class="page">
		<top></top>
		<headlines :list="headlinesList"></headlines>
		<field></field>
		<activity :list="activityList" v-if="activityList.length"></activity>
		<my-video :list="bannerList"></my-video>
		<news :list="newsList"></news>
		<teams></teams>
	</view>
</template>

<script>
	import Top from './components/Top/index.vue'
	import Headlines from './components/Headlines/index.vue'
	import Field from './components/Field/index.vue'
	import Activity from './components/Activity/index.vue'
	import MyVideo from "./components/Video/index.vue"
	import News from "./components/News/index.vue"
	import Teams from "./components/Teams/index.vue"

	export default {
		components: {
			Top,
			Headlines,
			Field,
			Activity,
			MyVideo,
			News,
			Teams
		},
		data() {
			return {
				activityList: [],
				bannerList: [],
				headlinesList: [],
				newsList: []
			}
		},
		watch: {
			'store_areaInform.areaCode'(newValue, oldValue) {
				this.loadNews();
				this.loadHeadlines();
			}
		},
		created() {
			this.allRequest();
		},
		async onReady() {
			uni.$emit('system-info', this.systemInfo);
		},
		async onPullDownRefresh() {
			this.allRequest();
			await uni.stopPullDownRefresh();
		},
		methods: {
			toLoginPage() {
				this.navigateTo({
					url: '../../main/login/password'
				})
			},
			async getActivityList() {
				const result = await this.$api.queryIndexActiveList({
					areaCode: '',
					pageSize: 3
				});
				console.log(1111, result)
				if (result.resultCode === '200') {
					this.activityList = result.data?.data??[];
				}
			},
			async allRequest() {
				await this.getActivityList(); // 获取热点活动
				await this.loadBannerList(); //获取轮播
				await this.loadPosition();
				await this.loadNews();
				await this.loadHeadlines();
			},
			//请求资讯
			async loadNews() {
				const result = await this.$api.queryNewsList({
					areaCode: this.store_areaInform.areaCode, //	区域编码（切换到的区域的编码）
					cpage: 1,
					pageSize: 3,
					isTop: 1 //置顶
				});
				if (result.resultCode === '200' && result.data.data) {
					this.newsList = result.data.data.recordList;
				}
			},
			//请求头条数据
			async loadHeadlines() {
				const result = await this.$api.queryHeadNewsList({
					areaCode: this.store_areaInform.areaCode,
					cpage: 1,
					pageSize: 3
				});
				if (result.resultCode === '200' && result.data.data) {
					this.headlinesList = result.data.data.recordList;
				}
			},
			async loadBannerList() {
				const result = await this.$api.queryBannerList({
					termCode: 1,
					parentCode: "P1",
					bannerType: 0,
					pageSize: 3,
					areaCode: this.store_areaInform.areaCode,
					type: 1
				});
				if (result.resultCode === '200' && result.data.data) {
					this.bannerList = result.data.data.recordList || [];
				}
			},
			async loadPosition() {
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					success: async (res) => {
						if (res.address) {
							const result = await this.$api.getAreaByLocation({
								longitude: res.address.longitude,
								latitude: res.address.latitude
							});
							if (result.resultCode === '200' && result.data.data) {
								let obj = result.data.data.default;
								let inform = {
									areaCode: "",
									cityName: "北京"
								};
								for (let key in obj) {
									inform.areaCode = key;
									inform.cityName = obj[key]
								}
								this.changeAreaCode(inform);
							}
						}
					},
					fail: (res) => {
						console.log('定位错误', res);
					}
				});

			},
			async changeAreaCode(inform) {

				this.$store.dispatch('changeAreaCode', inform).then(res => {
					console.log('成功')
				}).catch(err => {

				})
			}
		},

	}
</script>

<style lang="scss">
	@import "../../../commom/styles/icons.css";

	page {
		background: #F4F5F9;
	}
</style>
